package com.sunrise.xcrm.ui.list.pagingcomponent.demo;

import com.sunrise.xcrm.ui.list.pagingcomponent.ExplicitPagingContainer;
import com.sunrise.xcrm.ui.list.pagingcomponent.HiddenPagingContainer;
import com.sunrise.xcrm.ui.list.pagingcomponent.PagingComponent;
import com.sunrise.xcrm.ui.list.pagingcomponent.PagingModel;
import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Component;
import com.vaadin.ui.Table;
import com.vaadin.ui.Panel;
import com.vaadin.ui.HorizontalLayout;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

public class PagingComponentDemo extends Application {

  PagingModel model = new PagingModel(50);
  PagingComponent pagingComponent = new PagingComponent(model);
  Label lCurrentPage = new Label("Current Page: ?");
  private ExplicitPagingContainer explicitPagingContainer;

  public void init() {
    setTheme("paging");

    Component hiddenPaging = createHiddenPaging();
    Component explicitPaging = createExplicitPaging();

    VerticalLayout mainLayout = new VerticalLayout();

    HorizontalLayout pagingExamples = new HorizontalLayout();
    pagingExamples.setMargin(true);
    pagingExamples.setSpacing(true);
    pagingExamples.setSizeFull();
    pagingExamples.addComponent(hiddenPaging);
    pagingExamples.addComponent(explicitPaging);

    mainLayout.addComponent(pagingExamples);
    
    setMainWindow(new Window("Paging Demonstrating", mainLayout));

  }

  private Component createExplicitPaging() {
    VerticalLayout layout = new VerticalLayout();
    layout.setSizeFull();
    layout.setMargin(true);
    layout.setSpacing(true);
    Table table = new  Table();
    explicitPagingContainer = new ExplicitPagingContainer();
    table.setContainerDataSource(explicitPagingContainer);
    table.setSizeFull();
    layout.addComponent(table);
    layout.addComponent(pagingComponent);
    layout.addComponent(lCurrentPage);

    explicitPagingContainer.loadPage(1);
    updatePageNumber();

    model.addPropertyChangeListener("currentPage", new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        updatePageNumber();
        explicitPagingContainer.loadPage(model.getCurrentPage());
      }
    });

    Panel explicitPaging = new Panel("Explicit Paging");
    explicitPaging.setContent(layout);
    explicitPaging.setSizeFull();
    return explicitPaging;
  }

  private Component createHiddenPaging() {
    Table table = new  Table();
    table.setContainerDataSource(new HiddenPagingContainer());
    table.setSizeFull();
    Panel hiddenPaging = new Panel("Hidden Paging, Crouching UI Waits");
    hiddenPaging.addComponent(table);
    hiddenPaging.setSizeFull();
    return hiddenPaging;
  }

  private void updatePageNumber() {
    lCurrentPage.setValue("Current Page : " + model.getCurrentPage());
  }
}
